﻿import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text ,RichText } from '@tarojs/components'
import CommonJs from '../../utils/common'
import '../../assets/common.scss'
class Hearder extends Component{
 constructor(props) {
    super(props);
    this.state = {
		userinfo:Taro.getStorageSync('userinfo'),
      result: [],
      myTime:null,
      navwidth:0
    }
  }

  componentWillMount () {
    // console.log(JSON.stringify(this.state.userinfo))
    var _this = this
    var navwidths = Taro.getStorageSync('navwidths')
    // console.log(navwidths+'//1111111111111111111111')
    if (navwidths != '' || navwidths != 'undefined') {
      if (navwidths == 1) {
        Taro.getSystemInfo({
          success:function(res){
            var windowWidth = res.windowWidth
            var navwidth = windowWidth / 6
            // console.log(navwidth+'2222222222222222222222')
            _this.setState({
              navwidth:navwidth
            })
          }
        }) 
      }
    }
  }
  componentDidShow () {
    var _this = this
    var navwidths = ''
    Taro.setStorage({key:'navwidths',data:navwidths})
    var chatRoomTypeIds
    // console.log(_this.props.porpNav+'////1111111111111111111111111')
    if(_this.props.porpNav == 1 || _this.props.porpNav == 6 || _this.props.porpNav == 9){
      chatRoomTypeIds = 2
    }else if(_this.props.porpNav == 3){
      chatRoomTypeIds = 4
    }else if(_this.props.porpNav == 4){
      chatRoomTypeIds = 5
    }else if(_this.props.porpNav == 7){
      chatRoomTypeIds = 6
    }
    var cid = Taro.getStorageSync('dateinfo').qid
    // console.log(Taro.getStorageSync('dateinfo')+'yy000000000000')
    if (cid != ''){
      // console.log('gfgfgfgffffffffffffffff')
      //登陆没有群id跳群列表判断因为活动头部会请求
      _this.getChatHistory(chatRoomTypeIds)
    }
  }
  shouldComponentUpdate () {
  }
  componentWillUnmount () {
    clearTimeout(this.state.myTime)
    // clearInterval(this.myTime1)
  }

  opens(url,type,types){
    var navwidths = types
    Taro.setStorage({key:'navwidths',data:navwidths})
    // clearInterval(this.myTime)
    //缓存一个值判断横向是否滚动一段距离
    
    this.props.onHandOpen(url,type)
  }
  opendetails(url){
    CommonJs.openTo(url);
  }

  openchat(url,id){
    var _this=this
    if(_this.props.porpNav == 1 || _this.props.porpNav == 6 || _this.props.porpNav == 9){
      _this.$preload({
        'chatRoomTypeId':2
      });
    }else if(_this.props.porpNav == 3){
      _this.$preload({
        'chatRoomTypeId':4
      });
    }else if(_this.props.porpNav == 4){
      _this.$preload({
        'chatRoomTypeId':5
      });
    }else if(_this.props.porpNav == 7){
      _this.$preload({
        'chatRoomTypeId':6
      });
    }
    CommonJs.openTo(url);
  }
  
  getChatHistory(obj){
    //获取群聊天历史记录
    var _this=this
    var dates
     dates={
        clusterId:Taro.getStorageSync('dateinfo').qid,
        pageNumber:1,
        sortColumns:'id desc',
        pageSize:3,
        chatRoomTypeId: obj
      }
    // console.log(_this.props.porpNav+'oooooooooo1111111888888888888')
    // console.log(JSON.stringify(dates)+'1111111888888888888')
    CommonJs.ajaxRequest('/clusterlog/getChatlogOrNotLogin',dates,'GET',function(res){
        // console.log(JSON.stringify(res)+'111111199999999999')
        if (res) {
          var results = res.result
          var result = results.reverse()
          _this.setState({
             result: result
          })
         var myTime = setTimeout(() => { _this.getChatHistory(obj) }, 6000)
          // _this.roll()
        } else {
         var myTime = setTimeout(() => { _this.getChatHistory(obj) }, 6000)
        } 
        _this.setState({
            myTime: myTime
        })
    },'noloading')
  }
  roll(){
    //轮播暂时没用
    var _this = this
    var lbheight = 0
    _this.myTime1 = setInterval(() => {
      lbheight = lbheight - 62
      console.log('yyyyyyy')
      if (lbheight == 248) {
        _this.setState({
          lotnlnbotop: 0 
        }) 
      } else {
        _this.setState({
          lotnlnbotop: lbheight 
        })
      }
    }, 1500)
    setTimeout(() => { _this.getChatHistory(obj) }, 6000)
  }
  changeyhq(item){
    let str = ''
    let contents = item.content
    let index = contents.indexOf("|||")//是否包含
    let bosh = null
    let markUrl = null
    let markUrls = []
    let sendObjectId = item.sendObjectId
    if(sendObjectId == 11){//群管后台活动列表分享活动 
      markUrl = item.markUrl
    }
    if(markUrl){//群管后台活动列表分享活动 
      markUrls = markUrl.split("|||")
    }
    if(index >= 0){
      bosh = contents.split("|||")
    }
    if (markUrls[0] == 1012 && markUrls[1] == "project") {
      str = '活动'
    } else if (index < 0) {
      str = contents
    } else if (bosh[1] == "pic") {
      str = '图片'
    } else if (bosh[1] == "ActivityShare") {
      str = '活动'
    } else if (bosh[1] == "pkgShare") {
      str = '商品'
    } else if(bosh[1] == "yuyin") {
      str = '语音'
    } else if(bosh[1] == "freegouShare") {
      str = '商品'
    } else {
      str = contents
    }
    return str
  }
  render () {
    const qun_date = this.props.propItem
    const navindex = this.props.porpNav
    const result = this.state.result
    //<Text className={navindex==10?'nav cur':'nav'} onClick={this.opendetails.bind(this,'/pages/mybill/mybill')}>我要买单</Text>
    const userid = Taro.getStorageSync('dateinfo').userid
    return(
      <View className='qun-commm'>
        <View className='qun-banner'>
          <View className='qun-ban'>
            <View className='qun-lotngs'>
              <View className='qun-lotngzg' onClick={this.openchat.bind(this,'/pages/chat/chat',navindex)}>
                <View className='qun-lotnlunbo'>
                  {result.map((item, index) => (
                   <View className={index == 0 ?'qun-lotnyito qun-lotnleft':'qun-lotnyito qun-lotnright'}>
                    <Image className='qun-lotnimgs' mode='scaleToFill' src={item.photo?item.photo:'https://minichat.g2ma.com/kedouimg/A6984900727459logo.png'}></Image>
                    <View className='qun-lotninfo'>{this.changeyhq(item)}</View>
                   </View>
                  ))}
                </View>
              </View>
            </View>
            <Image className='ban-img' mode='widthFix' src={qun_date.backgroundImg ? qun_date.backgroundImg : 'https://minichat.g2ma.com/kedouimg/004.png'}></Image>
          </View>
          <Image className='qun-img' mode='scaleToFill' src={qun_date.photo?qun_date.photo:'https://minichat.g2ma.com/kedouimg/A6984900727459logo.png'}></Image>
          <View className='qun-tit'>
            <View>
              <Text className='qun-cynum'>群聊人数:{qun_date.clusterCount}</Text>
              <Text className='qun-cyno'>社群编号:{qun_date.id}</Text>
            </View>
            <View>
              <Button openType='share' className='qun-fnxn buttons'>
                <Image className='qun-fnxnimg' mode='scaleToFill' src='https://minichat.g2ma.com/kedouimg/40.png'></Image>
                <Text className='qun-fxwn'>分享社群</Text>
             </Button>
            </View>
          </View>
          <View className='qun-funbor'>
            <ScrollView 
              className='qun-fung' 
              scrollX='true'
              scrollLeft={this.state.navwidth}>
              <View className='qun-fun'>
                <Text className={navindex==4?'nav cur':'nav'} onClick={this.opens.bind(this,'/pages/activity/activity?scene='+qun_date.id,'','0')}>活动</Text>
                <Text className={navindex==1?'nav cur':'nav'} onClick={this.opens.bind(this,'/pages/index/index','1','0')}>动态</Text>
                <Text className={navindex==9?'nav cur':'nav'} onClick={this.opens.bind(this,'/pages/pkgo/lottery','','0')}>抽奖</Text>
                <Text className={navindex==3?'nav cur':'nav'} onClick={this.opens.bind(this,'/pages/index/index','2','1')}>说一说</Text>
                <Text className={navindex==7?'nav cur':'nav'} onClick={this.opens.bind(this,'/pages/pkgo/pkgo','','1')}>免费购</Text>
                <Text className={navindex==6?'nav cur':'nav'} onClick={this.opens.bind(this,'/pages/info/info','','1')}>群资料</Text>
                <Text className={navindex==10?'nav cur':'nav'} onClick={this.opendetails.bind(this,'/pages/mybill/mybill')}>我要买单</Text>
                <Text className={navindex==8?'nav cur':'nav'} onClick={this.opendetails.bind(this,'/pages/my/my')}>我的</Text>
              </View>
            </ScrollView>
          </View>
        </View>
      </View>
    )
  }
}